#include <iostream>
#include <GL/glew.h>
#include "Framework.h"
#include "IniParser.h"
#include "WindowStatus.h"
#include "main.h"

void KeyboardFunc(unsigned char key, int x, int y) ;
void SpecialKeyFunc(int key, int x, int y) ;

bool FrameworkInit(int* argc, char** argv) {
    std::cout << "FrameworkInit." << std::endl;
	glutInit(argc, argv);

    int w = WindowStatus::getSingletonPtr()->w;
    int h = WindowStatus::getSingletonPtr()->h;

    IniParser parser("config.ini");
    parser.dump(stderr);
    w = parser.getint("screen settings:width", -1);
    h = parser.getint("screen settings:height", -1);

    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
    glutInitWindowSize(w, h);
    glutInitWindowPosition(0, 0);
    WindowStatus::getSingletonPtr()->window = glutCreateWindow("SpaceSimulator");

    glutDisplayFunc(MainLoop);
    glutIdleFunc(MainLoop);
    glutReshapeFunc(FrameworkResize);
    glutKeyboardFunc(KeyboardFunc);
    glutSpecialFunc(SpecialKeyFunc);


	GLenum err = glewInit();
	if (GLEW_OK != err)
	{
		/* glewInit初始化失败 */
		std::cerr << "Error: " << glewGetErrorString(err) << std::endl;
		FrameworkQuit();
	}
	std::cout << "Status: Using GLEW :" << glewGetString(GLEW_VERSION) << std::endl;
	if (glGetError() == GL_NO_ERROR) {
		std::cout << "初始化完成," << std::endl;
	} else {
		std::cout << "OpenGL initialize faild!" << std::endl;
		FrameworkQuit();
	}
    return true;
}

void FrameworkQuit(void) {
    std::cout << "FrameworkQuit." << std::endl;
    glutDestroyWindow(WindowStatus::getSingletonPtr()->window);
    exit(0);
}

void FrameworkMainLoop(void) {
    std::cout << "FrameworkMainLoop." << std::endl;
    glutMainLoop();
}

void FrameworkResize(int width, int height) {
    if (width == 0 && height == 0) {
        return;
    }
    int& w = WindowStatus::getSingletonPtr()->w;
    int& h = WindowStatus::getSingletonPtr()->h;

    w = width, h = height;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glViewport(0, 0, w, h);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0f, (GLfloat)w / (GLfloat)h, 5.0f, 10000.0f);

    glutPostRedisplay();
}

void FrameworkEvent(void) {
    return;
}

long Framework_GetTicks(void) {
    static long tick;
    return (tick += 10);
}

void FrameworkSwapBuffers(void) {
    glutSwapBuffers();
}

void keyboard_s(int key, int x, int y) {
    
    switch(key) {
        default:
            break;
    }
}

void KeyboardFunc(unsigned char key, int x, int y) {
	usleep(100);

    KeyboardHandle(key, x, y);
}

void SpecialKeyFunc(int key, int x, int y) {
	usleep(100);
    
	switch(key) {
		case GLUT_KEY_PAGE_UP:
            SpecialKeyboardHandle(key, x, y);
			break;
		case GLUT_KEY_PAGE_DOWN:
            SpecialKeyboardHandle(key, x, y);
			break;
		case GLUT_KEY_UP:
            SpecialKeyboardHandle(key, x, y);
			break;
		case GLUT_KEY_DOWN:
            SpecialKeyboardHandle(key, x, y);
			break;
		case GLUT_KEY_RIGHT:
            SpecialKeyboardHandle(key, x, y);
			break;
		case GLUT_KEY_LEFT:
            SpecialKeyboardHandle(key, x, y);
			break;
		default:
			break;
	}
}

void FrameworkAudioPlayWave(char* file) {
    return;
}
